<template>
  <div>
    <h2>当前用户信息</h2>
    <button @click="fetchUserInfo">获取用户信息</button>
    <div v-if="userInfo">
      <p>用户名: {{ userInfo.username }}</p>
      <p>角色: {{ userInfo.position }}</p>
      <p>联系方式: {{ userInfo.phone }}}</p>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      userInfo: null,
    };
  },
  methods: {
    async fetchUserInfo() {
      const token = localStorage.getItem('token');
      try {
        const response = await axios.get('/auth/current', {
          headers: {
            Authorization: `Bearer ${token}`,
          },
        });
        this.userInfo = response.data.data;
      } catch (error) {
        alert('获取用户信息失败');
      }
    },
  },
};
</script>